package com.moxiao.yfjh.module.order.controller.admin.blindTradeRefund;

import com.moxiao.yfjh.framework.common.pojo.CommonResult;
import com.moxiao.yfjh.framework.common.pojo.PageResult;
import com.moxiao.yfjh.framework.excel.core.util.ExcelUtils;
import com.moxiao.yfjh.framework.operatelog.core.annotations.OperateLog;
import com.moxiao.yfjh.module.activity.dal.dataobject.activityInfo.ActivityInfoDO;
import com.moxiao.yfjh.module.activity.service.activityEnroll.ActivityEnrollService;
import com.moxiao.yfjh.module.activity.service.activityInfo.ActivityInfoService;
import com.moxiao.yfjh.module.activity.service.activityenrollverificate.ActivityEnrollVerificateService;
import com.moxiao.yfjh.module.blind.service.member.BlindMemberUserService;
import com.moxiao.yfjh.module.order.controller.admin.blindTradeRefund.vo.*;
import com.moxiao.yfjh.module.order.controller.app.vo.AppRefund;
import com.moxiao.yfjh.module.order.convert.blindTradeRefund.BlindTradeRefundConvert;
import com.moxiao.yfjh.module.order.dal.dataobject.blindTradeRefund.BlindTradeRefundDO;
import com.moxiao.yfjh.module.order.tradeRefund.blindTradeRefund.BlindTradeRefundService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;
import java.util.Objects;

import static com.moxiao.yfjh.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.moxiao.yfjh.framework.common.pojo.CommonResult.success;
import static com.moxiao.yfjh.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;
import static com.moxiao.yfjh.framework.operatelog.core.enums.OperateTypeEnum.UPDATE;
import static com.moxiao.yfjh.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
import static com.moxiao.yfjh.module.activity.enums.ErrorCodeConstants.ENROLL_NO_RELATED_ACTIVITIES_FOUND_NOT_EXISTS;
import static com.moxiao.yfjh.module.activity.enums.ErrorCodeConstants.ENROLL_REFUND_NOT_FOUND_NOT_EXISTS;

@Tag(name = "管理后台 - 相亲订单退款")
@RestController
@RequestMapping("/order/blind-trade-refund")
@Validated
public class BlindTradeRefundController {
    @Resource
    private BlindTradeRefundService blindTradeRefundService;
    @Resource
    private ActivityEnrollService enrollService;
    @Resource
    private ActivityInfoService infoService;
    @Resource
    private ActivityEnrollVerificateService verificateService;
    @Resource
    private BlindMemberUserService userService;


    @PostMapping("/create")
    @Operation(summary = "创建相亲订单退款")
    @PreAuthorize("@ss.hasPermission('order:blind-trade-refund:create')")
    public CommonResult<Long> createBlindTradeRefund(@Valid @RequestBody BlindTradeRefundCreateReqVO createReqVO) {
        return success(blindTradeRefundService.createBlindTradeRefund(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新相亲订单退款")
    @PreAuthorize("@ss.hasPermission('order:blind-trade-refund:update')")
    public CommonResult<Boolean> updateBlindTradeRefund(@Valid @RequestBody BlindTradeRefundUpdateReqVO updateReqVO) {

        blindTradeRefundService.updateBlindTradeRefund(updateReqVO);
        return success(true);
    }


    @DeleteMapping("/delete")
    @Operation(summary = "删除相亲订单退款")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('order:blind-trade-refund:delete')")
    public CommonResult<Boolean> deleteBlindTradeRefund(@RequestParam("id") Long id) {
        blindTradeRefundService.deleteBlindTradeRefund(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得相亲订单退款")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('order:blind-trade-refund:query')")
    public CommonResult<BlindTradeRefundRespVO> getBlindTradeRefund(@RequestParam("id") Long id) {
        BlindTradeRefundDO blindTradeRefund = blindTradeRefundService.getBlindTradeRefund(id);
        return success(BlindTradeRefundConvert.INSTANCE.convert(blindTradeRefund));
    }

    @GetMapping("/list")
    @Operation(summary = "获得相亲订单退款列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('order:blind-trade-refund:query')")
    public CommonResult<List<BlindTradeRefundRespVO>> getBlindTradeRefundList(@RequestParam("ids") Collection<Long> ids) {
        List<BlindTradeRefundDO> list = blindTradeRefundService.getBlindTradeRefundList(ids);
        return success(BlindTradeRefundConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得相亲订单退款分页")
    @PreAuthorize("@ss.hasPermission('order:blind-trade-refund:query')")
    public CommonResult<PageResult<BlindTradeRefundRespVO>> getBlindTradeRefundPage(@Valid BlindTradeRefundPageReqVO pageVO) {
        PageResult<BlindTradeRefundRespVO> page = blindTradeRefundService.getJoinTradeRefundPage(pageVO);
        return success(page);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出相亲订单退款 Excel")
    @PreAuthorize("@ss.hasPermission('order:blind-trade-refund:export')")
    @OperateLog(type = EXPORT)
    public void exportBlindTradeRefundExcel(@Valid BlindTradeRefundExportReqVO exportReqVO,
                                            HttpServletResponse response) throws IOException {
        List<BlindTradeRefundDO> list = blindTradeRefundService.getBlindTradeRefundList(exportReqVO);
        // 导出 Excel
        List<BlindTradeRefundExcelVO> datas = BlindTradeRefundConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "相亲订单退款.xls", "数据", BlindTradeRefundExcelVO.class, datas);
    }

    /**
     * 活动退款审核
     */
    @PostMapping("/auditTradeRefund")
    @Operation(summary = "活动退款审核")
    @OperateLog(type = UPDATE)
    @PreAuthorize("@ss.hasPermission('trade:refund:audit')")
    public CommonResult<Boolean> auditTradeRefund(@Valid @RequestBody BlindTradeRefundUpdateReqVO updateReqVO) {
        BlindTradeRefundDO vo = blindTradeRefundService.getBlindTradeRefund(updateReqVO.getId());
        if (Objects.nonNull(vo)) {
            LocalDateTime now = LocalDateTime.now();
            updateReqVO.setAuditTime(now);
            updateReqVO.setAuditReason(updateReqVO.getRejectReason());
            updateReqVO.setStatus(updateReqVO.getStatus());
            updateReqVO.setAuditUserId(getLoginUserId());
            // 参数传递
            updateReqVO.setActiveNo(vo.getActiveNo());
            updateReqVO.setUserId(vo.getUserId());
            blindTradeRefundService.auditBlindTradeRefund(updateReqVO);
        } else {
            throw exception(ENROLL_REFUND_NOT_FOUND_NOT_EXISTS);
        }
        return success(true);
    }

    /**
     * 退款-关闭活动
     */
    @PostMapping("/getCloseActivityInfo")
    @Operation(summary = "关闭活动（报名退款）")
    public CommonResult<Boolean> getCloseActivityInfo(@Valid @RequestBody AppRefund appRefund) {
        ActivityInfoDO info = infoService.getInfo(appRefund.getId());
        if (Objects.isNull(info)) {
            //未找到活动时返回错误提示
            throw exception(ENROLL_NO_RELATED_ACTIVITIES_FOUND_NOT_EXISTS);
        }
        Boolean t = blindTradeRefundService.getCloseActivityInfo(appRefund);
        return success(true);
    }

}
